//
//  NNHomeViewController.swift
//  CommonTool
//
//  Created by tenroadshow on 12.1.22.
//

import UIKit


public enum RowType: Int {
    case rollLabel = 0, waterFlow, textFieldRestrictions, imageAddLogo, guideWhenInstalled, caramra, alert, proxy, inputView, defender, cover

    static var numberOfRows: Int { RowType.cover.rawValue + 1 }
    
    public var title: String {
        switch self {
        case .rollLabel:
            return "跑马灯"
        case .waterFlow:
            return "瀑布流"
        case .textFieldRestrictions:
            return "输入框限制"
        case .imageAddLogo:
            return "图片添加logo"
        case .guideWhenInstalled:
            return "新安装引导"
        case .caramra:
            return "相机"
        case .alert:
            return "弹窗"
        case .proxy:
            return "NNWeakProxy"
        case .inputView:
            return "inputView"
        case .defender:
            return "防护"
        case .cover:
            return "封面"
        }
    }
}




class NNHomeViewController: NNBaseViewController {
    fileprivate lazy var tableView: UITableView = {
        let tableView = UITableView.defaultTableView()
        tableView.dataSource = self
        tableView.delegate   = self
        tableView.register(NNHomeTableViewCell.self,
                           forCellReuseIdentifier: NNHomeTableViewCell.reuseId)
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Home"
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}


extension NNHomeViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return RowType.numberOfRows
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let contentCell = tableView.dequeueReusableCell(withIdentifier: NNHomeTableViewCell.reuseId) as! NNHomeTableViewCell
        contentCell.content = (RowType(rawValue: indexPath.row) ?? RowType.rollLabel).title
        return contentCell
    }
}

extension NNHomeViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let type = RowType(rawValue: indexPath.row) else { return }
        
        let targetController: UIViewController
        switch type {
        case .rollLabel:
            targetController = NNRollLabelController()
        case .waterFlow:
            targetController = NNWaterFlowLayoutController()
        case .textFieldRestrictions:
            targetController = NNTextFieldViewController()
        case .imageAddLogo:
//            targetController = NNImageEditController()
            targetController = NNClipViewController()
        case .guideWhenInstalled:
            targetController = NNGuideViewController()
        case .caramra:
            if #available(iOS 13, *) {
                NNCaramaController.show()
            }
            return
        case .alert:
            targetController = NNAlertListController()
        case .proxy:
            targetController = NNWeakProxyController()
        case .inputView:
            targetController = NNInputViewController()
        case .defender:
            targetController = NNDefenderController()
        case .cover:
            targetController = TNAutoCoverController()
        }
        navigationController?.pushViewController(targetController, animated: true)
    }
}

class NNHomeTableViewCell: UITableViewCell {
    
    fileprivate lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 15)
        label.textColor = UIColor.random
        return label
    }()
    
    public var content: String? {
        didSet {
            contentLabel.text = content
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        selectionStyle = .none
        contentView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20))
        }
    }
}
